//
//  VCTransport.m
//  express
//
//  Created by mac1 on 15/9/7.
//  Copyright (c) 2015年 longchen. All rights reserved.
//

#import "VCTransport.h"

#import "DataCenter.h"

#import "CellTransOrder.h"

#import "VCPath.h"

#import "VCOrderDetail.h"

@interface VCTransport ()

@property (nonatomic, strong) DataCenter * data;

@property (nonatomic, strong) ZTTableView *tableView;

@property (nonatomic, strong) ZTTableView * tvDropList;

@property (nonatomic, strong) UIButton * btnDropTmp;

@end

@implementation VCTransport

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor redColor];
    
    //获取定单详情
    self.data = [DataCenter  Create];

    //添加菊花
    //[self addActivityIndicator ];
    
    [self.data getOrderDetail];
    
    [self addNavigation];
    
    [self addTableView];

}


#pragma mark 添加tableView
//添加一个tableView
-(void)addTableView
{
    
        self.tableView = [[ZTTableView alloc]initWithFrame:self.view.bounds withStyle:UITableViewStylePlain];
    
        [self.view addSubview:self.tableView];

        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
        self.tableView.showsHorizontalScrollIndicator = NO;

        self.tableView.backgroundColor = [UIColor colorWithRed:228/255.0    green:212/255.0  blue:216/255.0 alpha:1];

        [self.tableView setNumberOfSectionsInTableView:^NSInteger(UITableView *tableView) {
            return 1;
        }];

        [self.tableView setHeightForRowAtIndexPath:^CGFloat(NSIndexPath *indexPath) {
            return 200;
        }];

        [self.tableView setNumberOfRowsInSection:^NSInteger(NSInteger section) {
            return 12;
        }];

        [self.tableView setCustomizeCell:^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {

            [tableView registerNib:[UINib nibWithNibName:@"XibOrderTrans" bundle:nil] forCellReuseIdentifier:@"pool"];
                
            UITableViewCell * cell = cell = [ [ [ UINib nibWithNibName : @"XibOrderTrans" bundle : nil ] instantiateWithOwner : nil options : nil ]objectAtIndex:0];
            
            cell.contentView.backgroundColor = [UIColor colorWithRed:246/255.0 green:240/255.0 blue:217/255.0 alpha:1];
            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

            return cell;
            
        }];
    
    __weak typeof(self)  ws = self;
    [self.tableView setDidSelectRowAtIndexPath:^(UITableView *tableView, NSIndexPath *indexPath) {
        
        VCOrderDetail * vcOrderDetail = [[VCOrderDetail alloc]init];
        
        [ws.navigationController pushViewController:vcOrderDetail animated:YES];
        
    }];
    
    
    //为表头添加视图
    UIView * headerView = [[UIView alloc]init];
    
    headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 40);
    
    self.tableView.tableHeaderView = headerView;
    
    [self addDropBtn:headerView];

}

#pragma mark 添加下拉按钮
-(void)addDropBtn:(UIView*)view
{

    ZTButton * btnDest = [self createButtonWithRect:CGRectMake(0, 0, SCREEN_WIDTH/3, 40) withTitle:@"目的地" withDropData:@[@"1",@"2"]];
    
    [view addSubview:btnDest];
    

    ZTButton * btnNearby = [self createButtonWithRect:CGRectMake(SCREEN_WIDTH/3, 0, SCREEN_WIDTH/3, 40) withTitle:@"附近" withDropData:@[@"3",@"4"]];
    
    [view addSubview:btnNearby];
    

    ZTButton * btnStatus = [self createButtonWithRect:CGRectMake(SCREEN_WIDTH/3*2, 0, SCREEN_WIDTH/3, 40) withTitle:@"状态" withDropData:@[@"5",@"6"]];
    
    [view addSubview:btnStatus];

}


//创建按钮
-(ZTButton*)createButtonWithRect:(CGRect)rect withTitle:(NSString*)btnTitle withDropData:(NSArray*)arr
{

    ZTButton * btn = [[ZTButton alloc]initWithFrame:rect];

    [btn setTitle:btnTitle forState:UIControlStateNormal];

    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    btn.titleLabel.font = [UIFont systemFontOfSize:14];

    [btn setAction:^(ZTButton * btn) {

            btn.selected = !btn.selected;
            
            [self addDropList:btn.selected withRect:rect withData:arr];
        
            if(btn != self.btnDropTmp)
                self.btnDropTmp.selected = !self.btnDropTmp;
        
            //临时按钮指向当前按钮
            self.btnDropTmp = btn;

    }];


    return btn;
}


#pragma mark 添加下拉列表
-(void)addDropList:(BOOL)PopOrRelease withRect:(CGRect)rect withData:(NSArray*)arrData
{
    if(PopOrRelease)
    {
            if(self.tvDropList == nil)
            {

                self.tvDropList = [[ZTTableView alloc]initWithFrame:CGRectMake(rect.origin.x, 40, rect.size.width, arrData.count * 30) withStyle:UITableViewStylePlain];

                self.tvDropList.scrollEnabled = NO;

               // self.tvDropList.separatorStyle = UITableViewCellSeparatorStyleNone;
            }

            [self.view addSubview:self.tvDropList];

            self.tvDropList.frame = CGRectMake(rect.origin.x, 40, rect.size.width, arrData.count * 30 );

            [self.tvDropList  setNumberOfRowsInSection:^NSInteger(NSInteger section) {

                return arrData.count;

            }];

            [self.tvDropList setHeightForRowAtIndexPath:^CGFloat(NSIndexPath *indexPath) {

                return 30;

            }];

            [self.tvDropList  setAddContentIntoCell:^UITableViewCell *(UITableViewCell *cell, NSIndexPath *indexPath) {

                cell.selectionStyle = UITableViewCellSelectionStyleDefault;

                cell.textLabel.text = [arrData objectAtIndex:indexPath.row];

                cell.textLabel.textColor = RGBA(30, 30, 30, 1);

                return cell;

            }];

        [self.tvDropList reloadData];

    }
    else
    {
        [self.tvDropList removeFromSuperview];
    }

}


#pragma  mark 菊花
//添加菊花函数，菊花在数据中心申明，在VC中申请空间. 添加view,开始转动， 在
-(void)addActivityIndicator
{
    self.vActivityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    
    self.vActivityIndicator.center = CGPointMake(self.view.width/2,  self.view.height/2);
    
    [self.view addSubview:self.vActivityIndicator];
    
    [self.vActivityIndicator startAnimating];
    
    self.vActivityIndicator.hidesWhenStopped = YES;
    
    //数据准备完成后，关掉菊花
    __weak typeof (self) ws = self;
    
    [self.data setDataIsReady:^{
        
        [ws.vActivityIndicator stopAnimating];

    }];

}


#pragma  mark 导航栏
-(void)addNavigation
{
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:2/255.0 green:147/255.0 blue:95/255.0 alpha:1]];


    //创建 标题LABEL
    UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 60, 140, 20)];
    
    titleLabel.text = @"派件";
    
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    titleLabel.font = [UIFont systemFontOfSize:14];
    
    titleLabel.font = [UIFont boldSystemFontOfSize:20];//加粗
    
    titleLabel.textColor = [UIColor whiteColor];
    
    self.navigationItem.titleView = titleLabel;


    //创建左侧按钮,这样写代码重用性较高
    UIButton *leftButton  = [ UIButton  buttonWithType : UIButtonTypeSystem ];
     
    leftButton.frame = CGRectMake(0, 0, 30, 30);
    
    [leftButton setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    
    [leftButton addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    
    self.navigationItem.leftBarButtonItem = leftBarBtn;
    

    //创建右侧按钮,这样写代码重用性较高
    UIButton *rightButton  = [UIButton  buttonWithType:UIButtonTypeSystem];

    rightButton.frame = CGRectMake(0, 0, 60, 30);
    
    [rightButton addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [rightButton setTitle:@"路线规划" forState:UIControlStateNormal];
    
    rightButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    
    self.navigationItem.rightBarButtonItem = rightBarBtn;


    //设置模糊,默认YES
    [self.navigationController.navigationBar setTranslucent:NO];
    
}


-(void)leftBtnClick:(UIButton*)btnClick
{
    
     [self.navigationController popViewControllerAnimated:YES];
    
}


-(void)rightBtnClick:(UIButton*)btnClick
{
    
    VCPath * vcPath = [[VCPath alloc]init];
    
    [self.navigationController pushViewController:vcPath animated:YES];
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
